/*手风琴*/
;(function($){
    $.fn.accordion = function(){
        return this.each(function(){
            var $this = $(this);
            var $toggle = $this.find(".accordion-toggle").eq(0);
            $toggle.click(function(e){
                $this.find(".accordion-body").slideToggle("fast",function(){
                    $this.toggleClass("open");
                });
                e.preventDefault(); 
            });
        });
    }
    $(".accordion").accordion();
})(jQuery);
/*滑动条*/
;(function($){
    $.fn.slider = function(){
        return this.each(function(){
            var $this = $(this);
            $this.html('<div class="range"><div class="range-active"></div><span class="range-handle"></span></div>');
            
            var $range = $this.find(".range"),
                $handle = $this.find(".range-handle"),
                $active = $this.find(".range-active"),
                $value = $("#"+$this.attr("for")),
                $default =parseInt($this.attr("default"))||0;
            
            var width = $range.width(),
                range = $this.attr("range")||100;
            if($default >= 0 && $default <= range){
                var lastleft = Math.round($default/range*100);
                $handle.css("left",lastleft+"%");
                $active.css("width",lastleft+"%");
                $value.val($default);
            }
            
            $handle.mousedown(function(e){init(e);}).mouseup(function(){key = false;}).mousemove(function(e){move(e);});
            $(document).mouseup(function(){key = false;}).mousemove(function(e){move(e);});
            
            var x=0,startleft = 0,key = false;
            function init(e){
                x = e.pageX; 
                startleft = parseFloat($handle.css("left"));
                key = true;
            }
            function move(e){
                if(key){
                    var lastleft = e.pageX-x+startleft;
                    if(lastleft >= 0 && lastleft <= width){
                        lastleft = Math.round(lastleft/width*100);
                        $handle.css("left",lastleft+"%");
                        $active.css("width",lastleft+"%");
                        $value.val(range*lastleft/100);
                    }
                }
            }
            
        });
    }
    $(".slider").slider();
})(jQuery);
/*告警提示标签*/
;(function($){
    $.fn.warn = function(){
        return this.each(function(){
            var $this = $(this);
            $this.find(".close").click(function(){
                $this.fadeOut();
            });
        });
    }
    $(".warn").warn();
})(jQuery);
/*滚动条插件*/
;(function($){
    $.fn.jscroll = function(options){
        var opt = $.extend({
            speed:5,
            height:300
        },options||{});
        return this.each(function(){
            var $this = $(this);
            if($this.children().height()>opt.height){
                $this.wrapInner('<div class="jscroll-content"></div>').css({"height":opt.height+"px","width":"100%"});
                
                $this.append('<div class="jscroll-bar"><div class="jscroll-tool"></div></div>');
                
                var $content = $this.find(".jscroll-content"),
                    $tool = $this.find(".jscroll-tool");
            
                var per = $content.outerHeight()/opt.height;
                $tool.height(opt.height/per);
                var max = $content.outerHeight()-opt.height;
                $this.mousewheel(function(event,delta){
                    var cTop = parseFloat($content.css("top"));
                    if(delta>0){
                        if(cTop<=0){
                            $content.css("top",cTop+opt.speed*per+"px");
                            $tool.css("top",(parseFloat($tool.css("top"))-opt.speed)+"px");
                        }
                    }else{
                        if(-max<=cTop){
                            $content.css("top",cTop-opt.speed*per+"px");
                            $tool.css("top",(parseFloat($tool.css("top"))+opt.speed)+"px");
                        }
                        
                    }
                    return false;
                });
                var key = false,y=0,moveRange = 0,tTop = 0,cTop = 0;
                $tool.mousedown(function(e){
                    y=e.pageY;
                    key = true;
                    tTop = parseFloat($tool.css("top"));
                    cTop = parseFloat($content.css("top"));
                }).mouseup(function(){
                    key = false;
                }).mousemove(function(e){
                    moveTool(e);
                });
                $(document).mouseup(function(){key = false;}).mousemove(function(e){moveTool(e);});
                function moveTool(e){
                    if(key){
                        var moveY = e.pageY-y;
                        if(moveRange > moveY){
                            if(parseFloat($content.css("top"))<=0){
                                $content.css("top",cTop-moveY*per+"px");
                                $tool.css("top",tTop+moveY+"px");
                                moveRange = moveY;
                            }
                        }else if(moveRange < moveY){
                            if(-max<=parseFloat($content.css("top"))){
                                $content.css("top",cTop-moveY*per+"px");
                                $tool.css("top",tTop+moveY+"px");
                                moveRange = moveY;
                            }
                        }
                    }
                }
            }
        });
    }
    $(".jscroll-pane").jscroll();
})(jQuery);
/*全选*/
;(function(){
    $.fn.checkAll = function(){
        var $this = (this);
        var id=$this.attr("id");
        var key = id.substring(8)||"";
        $this.click(function(){
            $(".check"+key).prop("checked",$this.prop("checked"));
        });
        $(".check"+key).live("click",function(){
            if($this.prop("checked")){
                $this.prop("checked",$(this).prop("checked"));
            }else{
                var swit = true;
                $(".check"+key).each(function(){
                    if(!$(this).prop("checked")){
                        swit = false;
                        return false;
                    }
                });
                $this.prop("checked",swit);
            }
        });
    }
})(jQuery);
/*下拉菜单*/
;(function(){
	$.fn.dropdown = function(options){
		var $this = $(this);
		$this.find('.dropdown-toggle').live('click',function(e){
			$(this).parents('.dropdown').toggleClass("open");
			return false;
		});
		$this.find('.dropdown-menu').live('click',function(e){
			$(this).parents('.dropdown').toggleClass("open");
		});
		$(document).click(function(){
			$this.removeClass("open");
		});
		return $this;
	}
	$(".dropdown").dropdown();
})(jQuery);
/*选项卡*/
;(function(){
    $.fn.Tabs = function(options){
        var trigger = options || "click";
        return this.each(function(){
            var $this = (this);
            $(".tab-lists .tab-list",$this).live(trigger,function(e){
                var index;
                if(!$(this).hasClass("active")){
                    $(this).addClass("active").siblings().removeClass("active");
                    index =$(this).parent().find(".tab-list").index($(this));
                    $(".tab-contents .tab-content",$this).eq(index).addClass("active").siblings().removeClass("active");
                }
				e.preventDefault();
            });
        });
    }
    $(".tabs").Tabs();
})(jQuery);
/*弹出窗口插件*/
;(function($){
    $.fn.dialog = function(options){
        var $this = $(this);
        var defaults = {
            backdrop:true
        }
        var settings = $.extend({},defaults,options||{});
        var dialog = $.dialog;
        dialog.show($this,settings.backdrop);
        $this.find(".modal-close").click(function(){
            dialog.close($this,settings.backdrop);
            return false;
        });
        return $this;
    }
    $.dialog = {
        close:function(dialog,backdrop){
            if(backdrop){
                $(".backdrop").hide();
            }
            dialog.hide();
        },
        show:function(dialog,backdrop){
            if(backdrop){
                this.backdrop();
            }
            dialog.show();
        },
        backdrop:function(){
            var backdrop = $('<div class="backdrop"></div>').appendTo($('body'));
            var winWidth = $(document).width();//获取屏幕宽度
            var winHeight = $(document).height();//获取屏幕高度
            backdrop.width(winWidth);
            backdrop.height(winHeight);
            backdrop.css({
                "position":"fixed",
                "background":"#DDD",
                "filter":"alpha(opacity=60)",
                "opacity":0.6,
                "left":0,
                "top":0,
                "z-index":"8888",
                "display":"block"
            });
        }
    }
})(jQuery);
/*弹出提示*/


/*表单验证提示*/
function showNewUIErrorMsg(id,msg)
{
 	if(!id) return false;
 	$('#'+id).addClass('error');
 	$('#'+id).parent('div').nextAll('div[class*=form-handle]').html(msg);
}
function clernError()
{
	$('div[class*=form-handle]').each(function(){
		$(this).empty();
		$(this).prev('div').children().removeClass('error');
	});
	$('#msg_container').empty().hide();
}

